<?php
// 应用公共文件

// 设置服务注入
\think\facade\App::bind('think\Paginator', \bigDream\thinkPaginatorDriver\Bootstrap4::class);

/**
 * 获取模型搜索字段和数据
 * @param array|string $modelClass
 * @param string $method
 * @return array
 */
function getModelSearchData($modelClass, string $method = 'param')
{
    if (is_string($modelClass)) {
        $field = [];
        foreach(get_class_methods($modelClass) as $name) {
            if (0 === strpos($name, 'search') && 'Attr' === substr($name, -4)) {
                $name = substr($name, 6, -4);
                $name = strtolower(trim(preg_replace('/[A-Z]/', '_\\0', $name), '_'));
                $field[] = $name;
            }
        }
    } else {
        $field = $modelClass;
    }

    return array_filter(
        \think\facade\Request::only($field, $method, ''),
        function($value) {
            return is_array($value) ? (count($value) > 0) : ('' != trim($value));
        }
    );
}